// 全局常量 / Global constants for EPlantApp Web API client

const ENV = {
  API_BASE_URL: process.env.REACT_APP_API_BASE_URL || process.env.API_BASE_URL || '/api',
  NODE_ENV: process.env.NODE_ENV || 'development',
  SIGNALR_HUB_DEVICE_STATUS_URL: `${API_BASE_URL}/deviceStatusHub`  // 或者你实际的 Hub 路径
};

const API = Object.freeze({
  BASE_URL: ENV.API_BASE_URL,
  TIMEOUT_MS: 30_000,
  CONTENT_TYPE_JSON: 'application/json',
  AUTH_HEADER: 'Authorization',
});

const ROUTES = Object.freeze({
  ROOT: '/',
  LOGIN: '/login',
  LOGOUT: '/logout',
  DASHBOARD: '/dashboard',
  PLANTS: '/plants',
  PLANT_DETAIL: (id = ':id') => `/plants/${id}`,
  SENSORS: '/sensors',
  DEVICES: '/devices',
  USERS: '/users',
  SETTINGS: '/settings',
  NOT_FOUND: '/404',
});

const STORAGE_KEYS = Object.freeze({
  AUTH_TOKEN: 'eplant_auth_token',
  REFRESH_TOKEN: 'eplant_refresh_token',
  USER_PROFILE: 'eplant_user_profile',
  THEME: 'eplant_theme',
  LAST_SYNC: 'eplant_last_sync',
});

const DEFAULTS = Object.freeze({
  PAGE_SIZE: 20,
  PAGE_NUMBER: 1,
  DATE_FORMAT: 'yyyy-MM-dd', // used by date libraries; adjust as needed
  DATETIME_FORMAT: 'yyyy-MM-dd HH:mm:ss',
  CURRENCY: 'USD',
  LOCALE: 'en-US',
});

const ROLES = Object.freeze({
  ADMIN: 'Admin',
  OPERATOR: 'Operator',
  VIEWER: 'Viewer',
});

const HTTP_STATUS = Object.freeze({
  OK: 200,
  CREATED: 201,
  NO_CONTENT: 204,
  BAD_REQUEST: 400,
  UNAUTHORIZED: 401,
  FORBIDDEN: 403,
  NOT_FOUND: 404,
  CONFLICT: 409,
  SERVER_ERROR: 500,
});

const MESSAGES = Object.freeze({
  GENERIC_ERROR: '发生错误，请稍后重试。', // Chinese fallback
  NETWORK_ERROR: '无法连接到服务器，请检查网络。',
  UNAUTHORIZED: '未授权或会话已过期，请重新登录。',
  SAVE_SUCCESS: '保存成功。',
  DELETE_CONFIRM: '确定要删除此项吗？',
});

function buildApiUrl(path = '') {
  const trimmedBase = API.BASE_URL.replace(/\/+$/, '');
  const trimmedPath = String(path).replace(/^\/+/, '');
  return `${trimmedBase}/${trimmedPath}`;
}

const Constants = Object.freeze({
  ENV,
  API,
  ROUTES,
  STORAGE_KEYS,
  DEFAULTS,
  ROLES,
  HTTP_STATUS,
  MESSAGES,
  buildApiUrl,
});

export {
  ENV,
  API,
  ROUTES,
  STORAGE_KEYS,
  DEFAULTS,
  ROLES,
  HTTP_STATUS,
  MESSAGES,
  buildApiUrl,
};

export default Constants;
